package com.example.mypoi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.BusLineSearchOption;
import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private MapView mapView = null;
    private PoiSearch poiSearch;
    private BaiduMap mBaiduMap;
    private  LatLng mlatLng;
    private AutoCompleteTextView auto_txt;
    private SuggestionSearch suggestionSearch;
    private ArrayList<String> stringArrayList = new ArrayList<>();
    private ArrayAdapter<String> arrayAdapter;
    private String key;
    private TextWatcher watcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mapView.getMap();
        auto_txt = findViewById(R.id.auto_txt);

        //5.1 Sug检索  创建实例
        suggestionSearch = SuggestionSearch.newInstance();
        //5.2sug监听事件
        suggestionSearch.setOnGetSuggestionResultListener(new OnGetSuggestionResultListener() {
            @Override
            public void onGetSuggestionResult(SuggestionResult suggestionResult) {
                //5.3取所有检索数据
                List<SuggestionResult.SuggestionInfo> suggestions = suggestionResult.getAllSuggestions();
                if (suggestions == null) {
                    return;
                }
                stringArrayList.clear();
                for (int i = 0; i<suggestions.size(); i++) {
                    Log.e("for循环里的我", suggestions.get(i).getKey());
                    //将数据添加进列表
                    stringArrayList.add(suggestions.get(i).getKey());
                }
                arrayAdapter = new ArrayAdapter<>(MainActivity.this,
                        android.R.layout.simple_list_item_1, stringArrayList);

                auto_txt.setAdapter(arrayAdapter);
                auto_txt.removeTextChangedListener(watcher);
                auto_txt.setText(key);
                //设置文本框光标位置
                auto_txt.setSelection(key.length());
                auto_txt.addTextChangedListener(watcher);

            }
        });
        watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                key = s.toString();
                suggestionSearch.requestSuggestion(new SuggestionSearchOption().city("广州")
                        .citylimit(true).keyword(key));
                Log.e("afterTextChanged",key);
            }
        };
        auto_txt.addTextChangedListener(watcher);

        //点击地图获取经纬度
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                //获取点击经纬度
                mlatLng = latLng;
                Log.e(TAG, latLng.toString());
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });

        //点击Marker事件
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                return false;
            }
        });

        //1.创建POI检索实例
        poiSearch = PoiSearch.newInstance();
        //2 创建POI检索监听器
        poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                //1.0PoiInfo 检索到的第一条信息
                Log.e(TAG, "onGetPoiResult=" + poiResult.getAllPoi().size());
                //3.3  遍历出所有数据
                for (int i = 0; i < poiResult.getAllPoi().size(); i++) {
                    PoiInfo poiInfo = poiResult.getAllPoi().get(i);
                    Log.e(TAG, poiInfo.getName() + " " + poiInfo.getAddress());
                }
                // 4.0 检索结果覆盖物  poiOverlay
                if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
                    mBaiduMap.clear();
                    //4.1  创建PoiOverlay对象
                    PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);
                    //4.2  设置Poi检索数据
                    poiOverlay.setData(poiResult);
                    //4.3　将poiOverlay添加至地图并缩放至合适级别
                    poiOverlay.addToMap();
                    poiOverlay.zoomToSpan();
                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
                // 2.2获取uid相关信息
                Log.e("onGetPoiDetailResult", poiDetailSearchResult.getPoiDetailInfoList().get(0).getName());
            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });
        //1.城市POI
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                poiSearch.searchInCity(new PoiCitySearchOption()
                        .city("北京").keyword("公园"));
                Log.e("onGetPoiResult", "点击了北京这个地方");
            }
        });

        //2.1通过uid检索数据
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                poiSearch.searchPoiDetail(new PoiDetailSearchOption().poiUids("0dc3d82dd994467574e5b6f5"));
            }
        });

        //3 POI周边检索
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //3.1 在searchBounds区域内检索餐厅
                poiSearch.searchNearby(new PoiNearbySearchOption()
                        .location(mlatLng).radius(1000).keyword("火锅"));
            }
        });
        //区域检索
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LatLng leftTop = new LatLng(mlatLng.latitude + 0.05, mlatLng.longitude - 0.05);
                LatLng rightBottom = new LatLng(mlatLng.latitude - 0.05, mlatLng.longitude + 0.05);
                //设置矩形检索区域
                LatLngBounds searchBounds = new LatLngBounds.Builder()
                        .include(leftTop)
                        .include(rightBottom)
                        .build();

                poiSearch.searchInBound(new PoiBoundSearchOption().bound(searchBounds).keyword("网吧"));
            }
        });

        //5  点击sug检索
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // searchBus();
                routePlan();
            }
        });
    }
        private void routePlan() {
            RoutePlanSearch routePlanSearch = RoutePlanSearch.newInstance();
            routePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
                @Override
                public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

                }

                @Override
                public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
                    Log.e(TAG, "size = " + transitRouteResult.getRouteLines().size());
                    //创建TransitRouteOverlay实例
                    TransitRouteOverlay overlay = new TransitRouteOverlay(mapView.getMap());
                    //获取路径规划数据,(以返回的第一条数据为例)
                    //为TransitRouteOverlay实例设置路径数据
                    if (transitRouteResult.getRouteLines().size() > 0) {
                        overlay.setData(transitRouteResult.getRouteLines().get(0));
                        //在地图上绘制TransitRouteOverlay
                        overlay.addToMap();
                        overlay.zoomToSpan();
                    }
                }

                @Override
                public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

                }

                @Override
                public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

                }

                @Override
                public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

                }

                @Override
                public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

                }
            });
            routePlanSearch.transitSearch(new TransitRoutePlanOption().city("广州")
                    .from(PlanNode.withCityNameAndPlaceName("广州","广东交通职业技术学院南校区"))
                    .to(PlanNode.withCityNameAndPlaceName("广州","广东交通职业技术学院北校区")));
        }

        private void searchBus() {
            PoiSearch poiSearch = PoiSearch.newInstance();
            poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
                @Override
                public void onGetPoiResult(PoiResult poiResult) {
                    Log.e(TAG, poiResult.getAllPoi().get(0).name + " uid = " + poiResult.getAllPoi().get(0).getUid() );
                    searchBusDetail(poiResult.getAllPoi().get(0).getUid());
                }

                @Override
                public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

                }

                @Override
                public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

                }

                @Override
                public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

                }
            });
            poiSearch.searchInCity(new PoiCitySearchOption().city("广州").keyword("28"));

        }

        private void searchBusDetail(String uid) {
            BusLineSearch busLineSearch = BusLineSearch.newInstance();
            busLineSearch.setOnGetBusLineSearchResultListener(new OnGetBusLineSearchResultListener() {
                @Override
                public void onGetBusLineResult(BusLineResult busLineResult) {
                    BusLineOverlay overlay = new BusLineOverlay(mapView.getMap());
                    overlay.setData(busLineResult);
                    overlay.addToMap();
                    overlay.zoomToSpan();
                }
            });

            busLineSearch.searchBusLine(new BusLineSearchOption().city("广州").uid(uid));

    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
    }
}
